import sys
import os.path
import urllib2
import httplib

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_301(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
        result.status = code
        return result

    def http_error_302(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
        result.status = code
        return result;

def get_reply(url):
    request = urllib2.Request(url)
    httplib.HTTPConnection.debuglevel = 1
    opener = urllib2.build_opener(SmartRedirectHandler())
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    f = opener.open(request)
    data = f.read()
    f.close()
    return data

def get_wiki_destination(phrase):
    search_phrase = phrase.replace(" ", "+")
    url = 'http://en.wikipedia.org/wiki/Special:Search?search=' + search_phrase
    data = get_reply(url)

    if 'There were no results matching the query.' in data:
        return 'ERROR'

    return url

def get_dict_destination(phrase):
        search_phrase = phrase.replace(" ", "+")
        url = 'http://dictionary.reference.com/browse/' + search_phrase

        data = get_reply(url)

        if 'no dictionary results' in data:
            return 'ERROR'

        return url


#class LinkDestination:
#    description = ''
#    is_valid = False
#    phrase = ''
#
#class WikiDestination(LinkDestination):
#    url = ''
#    search_phrase = ''
#    def __init__(self):
#        self.url = 'http://www.wikipedia.org/'
#        self.description = 'Wikipedia: '
#        self.search_phrase = ''
#
#    def create_url(self, new_phrase):
#        self.phrase = new_phrase
#        search_phrase = new_phrase.replace(" ", "+")
#        self.url = 'http://en.wikipedia.org/wiki/Special:Search?search=' + search_phrase
#        self.description = 'Wikipedia: ' + '\"' + new_phrase + '\"'
#
#        data = get_reply(self.url)
#
#        if 'There were no results matching the query.' in data:
#            self.url = 'Not Found'
#            self.is_valid = False
#        
#        
#        # check the url and update the description and
#        # is_valid flag
#
#    def __repr__(self):
#        return self.description
#
#class MWDestination(LinkDestination):
#    url = ''
#    search_phrase = ''
#    def __init__(self):
#        self.url = 'http://www.dictionary.com/'
#        self.description = 'Dictionary.com: '
#
#    def create_url(self, new_phrase):
#        self.phrase = new_phrase
#        search_phrase = new_phrase.replace(" ", "+")
#        self.url = 'http://dictionary.reference.com/browse/' + search_phrase
#        self.description = 'Dictionary.com: ' + '\"' + new_phrase + '\"'
#
#        data = get_reply(self.url)
#
#        if 'no dictionary results' in data:
#            self.url = 'Not Found'
#            self.is_valid = False
#        
#        
#        # check the url and update the description and
#        # is_valid flag
#
#    def __repr__(self):
#        return self.description
#
#class WebDestination(LinkDestination):
#    def __init__(self):
#        self.url = ''
#        self.description = 'Web: '
#
#    def create_url(self, url):
#        self.url = url
#        self.description = 'Web: ' + url
#
#    def set_phrase(self, phrase):
#        self.phrase = phrase
#        self.description = 'Web: ' + phrase + ': ' + self.description
#
#class NoteDestination(LinkDestination):
#    url = ''
#    def __init__(self):
#        self.url = 0
#        self.description = 'Note: '
#
#    def create_url(self, note_id):
#        self.url = 'http://note_id=' + note_id
#        self.description = 'Note: ' + note_id
#
#        # search for note in the database to check for validity?
#
#    def __repr__(self):
#        return self.description
